/**
 * Created by Administrator on 2017/4/25.
 */

var GameAI = require('../../game/ai').GameAI;
var MJ = require('./MJ');

exports.MJGameAI = GameAI.extend({
    init: function(player) {
        this._super(player);
    },

    onInitHandCards: function(msg) {
        if (msg.playerIndex != this.getPlayer().getIndex()) {
            return;
        }

        if (this.getRoom().isSelectColor()) {
            this.setAction(Ext.randRange(1500, 3000), function() {
                this._doSelectColor();
            });
        }
    },

    onPlayerColorSelected: function (msg) {
        if (this.getPlayer().getIndex() == this.getRoom().getCurPlay()) {
            this.setAction(Ext.randRange(1000, 2000), function() {
                var room = this.getRoom();
                var player = this.getPlayer();
                if (player.getIndex() == room.getCurPlay()) {
                    this._doPlayCard();
                }
            });
        }
    },

    onCurPlay: function(msg) {
        if (msg.curPlay != this.getPlayer().getIndex()) {
            return;
        }

        if (msg.newCard) {
            return;
        }

        this.setAction(Ext.randRange(1000, 2000), function() {
            var room = this.getRoom();
            var player = this.getPlayer();
            if (player.getIndex() == room.getCurPlay()) {
                this._doPlayCard();
            }
        });
    },

    onNewCard: function(msg) {
        if (msg.playerIndex != this.getPlayer().getIndex()) {
            return;
        }

        this.setAction(Ext.randRange(1000, 2000), function() {
            this._doPlayCard();
        });
    },

    onHangupTask: function(msg) {
        var tasks = msg.task;

        if (tasks.hasOwnProperty(MJ.Task.HU)) {
            this.setAction(Ext.randRange(800, 1500), function() {
                this.getRoom().callHandler("handleHuTask", this.getPlayer(), {});
            });
        } else {
            this.setAction(Ext.randRange(1500, 3000), function() {
                this.getRoom().callHandler("handlePassTask", this.getPlayer(), {});
            });
        }
    },

    onRoundSettlement: function(msg) {
        this.setAction(Ext.randRange(3000, 10000), function() {
            var room = this.getRoom();
            var player = this.getPlayer();

            var rand = Ext.rand(5);
            if (rand == 0) {
                room.callHandler("handleQuitRoom", player, {});
            } else {
                room.callHandler("handlePlayerReady", player, {});
            }
        });
    },

    _doSelectColor: function() {
        var room = this.getRoom();
        var player = this.getPlayer();

        var colorCardNum = {};
        room.getSubConfig().colors.forEach(function(color) {
            colorCardNum[color] = 0;
        });

        ExObject.eachKeyNum(player.getHandCards(), function(card, num) {
            ExObject.addNumber(colorCardNum, MJ.getColor(card), num);
        });
        delete colorCardNum[MJ.Color.ZI];
        delete colorCardNum[MJ.Color.HUA];

        var selectColor = Ext.randArray(Object.keys(colorCardNum));
        room.callHandler("handleSelectColor", player, {color: selectColor});
    },

    _doPlayCard: function() {
        var room = this.getRoom();
        var player = this.getPlayer();

        var playCard = player.getLastCard();
        if (room.isSelectColor()) {
            var handCards = player.getHandCards();
            ExObject.eachKeyNum(handCards, function(card, num) {
                if (MJ.getColor(card) == player.getSelectColor()) {
                    playCard = card; return false;
                }
            });
        }

        if (!playCard) {
            playCard = +Ext.randArray(Object.keys(player.getHandCards()));
        }

        room.callHandler("handlePlayCard", player, {card: playCard});
    },
});